//
//  SpotAnnView.m
//  ImGuider
//
//  Created by llt on 2017/6/23.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "SpotAnnView.h"
#import "IGAnnotation.h"

@interface SpotAnnView ()

@property (nonatomic) BOOL isShowingCallView;


//@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *numberBtn;

@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation SpotAnnView

- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self) {
        self.canShowCallout = NO;
        [self addSubview:self.numberBtn];
    }
    
    return self;
}

- (UIButton *)numberBtn {
    
    if (!_numberBtn) {
        _numberBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _numberBtn.backgroundColor = kThemeColor;
        _numberBtn.frame = CGRectMake(-14, -14, 28, 28);
        
        _numberBtn.layer.cornerRadius = 14;
        
        _numberBtn.layer.borderWidth = 2.0f;
        _numberBtn.layer.borderColor = [UIColor whiteColor].CGColor;
        
        _numberBtn.layer.shadowOffset = CGSizeZero;
        _numberBtn.layer.shadowOpacity = 0.4;
        _numberBtn.layer.shadowColor = [UIColor blackColor].CGColor;
        [_numberBtn addTarget:self action:@selector(annViewTapAction) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _numberBtn;
}

- (UIImageView *)bgImageView {
    
    
    if (!_bgImageView) {
        
        
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"annotation"]];
        //        _bgImageView.frame = CGRectMake(-7, -11, 42, 50);
        
        
        _imageView = [[UIImageView alloc] init];
        _imageView.frame = CGRectMake(1, 1, 40, 40);
        [self.bgImageView addSubview:_imageView];
        
        _imageView.layer.cornerRadius = 20.0f;
        _imageView.contentMode = UIViewContentModeScaleToFill;
        _imageView.clipsToBounds = YES;
    }
    return _bgImageView;
}

- (void)setAnnotation:(id<MKAnnotation>)annotation {
    [super setAnnotation:annotation];
    
    IGAnnotation *ann = (IGAnnotation *)annotation;
    
    [self.numberBtn setTitle:ann.infos[IGAnnotationNumberKey] forState:UIControlStateNormal];
    
}

- (void)annViewTapAction {
    
    if (self.annViewType == 1) {
        return;
    }
    
    if ([_delegate respondsToSelector:@selector(annotationClickView:)]) {
        [_delegate annotationClickView:self];
    }
    //    [self showCallView];
}


- (void)showCallView {
    
    IGAnnotation *ann = (IGAnnotation *)self.annotation;
    
    self.numberBtn.hidden = YES;
    [self addSubview:self.bgImageView];
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.numberBtn.mas_centerX);
        make.centerY.equalTo(self.numberBtn.mas_centerY).offset(-25);
        make.width.mas_equalTo(42);
        make.height.mas_equalTo(50);
    }];
    
    [self.imageView setImageURL:[NSURL URLWithString:ann.infos[IGAnnotationImageKey]] placeholderImage:[UIImage imageNamed:@"placeholder_squ"]];
}

- (void)hiddenCallView {
    
    [self.bgImageView removeFromSuperview];
    //    [self addSubview:self.numberBtn];
    self.numberBtn.hidden = NO;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    
    CGRect rect = self.bounds;
    BOOL isInside = CGRectContainsPoint(rect, point);
    if(!isInside) {
        
        for (UIView *view in self.subviews) {
            
            isInside = CGRectContainsPoint(view.frame, point);
            
            if (isInside) {
                break;
            }
        }
    }
    
    //    if (!isInside) {
    //
    //        [self hiddenCallView];
    //    }
    return isInside;
}

//如果监听到点击事件，就用 bringSubviewToFront 方法把self放在接收机的最前面，来接收这个事件。
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event {
    
    UIView *hitView = [super hitTest:point withEvent:event];
    if (hitView != nil) {
        
        [self.superview bringSubviewToFront:self];
    }
    return hitView;
}

@end

